package com.phpxiu.app.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.phpxiu.app.adapter.HomePagerAdapter;
import com.phpxiu.app.api.callback.MsgCountManager;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.event.OnShowHotFragment;
import com.phpxiu.app.view.C2CConversationManager;
import com.phpxiu.app.view.UserSearch;

import java.util.Observable;
import java.util.Observer;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;


/**
 * 主页界面
 * Created by WuJinZhou on 2016/5/5.
 */
public class HomeFragment extends BaseFragment implements View.OnClickListener,ViewPager.OnPageChangeListener,Observer {
    public final static int HANDLER_REFRESH_MSG_COUNT=0x115;//刷新未读消息数量
    public final static String TAG="HomeFragment";
    public final static String[] TITLE={"关注","热门","发现"};
    private ViewPager mViewPager;
    private HomePagerAdapter mAdapter;
    private TextView followMenu,hotMenu,discoverMenu;
    private RelativeLayout goSearchBtn;
    private RelativeLayout chatBtn;//私信按钮
    private TextView countView;
    private long msgCount=0;//未读私信条数

    public static BaseFragment builder(String arg){
        HomeFragment fragment=new HomeFragment();
        Bundle b = new Bundle();
        b.putString(ARG,arg);
        fragment.setArguments(b);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView=inflater.inflate(R.layout.home_fragment,null);
        init();
        initMsgCount();
        return rootView;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.home_fragment_navigator_bar_follow_menu:
                refreshIndicator(0);
                break;
            case R.id.home_fragment_navigator_bar_hot_menu:
                refreshIndicator(1);
                break;
            case R.id.home_fragment_navigator_bar_discover_menu:
                refreshIndicator(2);
                break;
            case R.id.home_fragment_navigator_bar_left_view:
                goSearch();
                break;
            case R.id.home_fragment_navigator_bar_right_view:
                showC2CListWin();
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        refreshIndicator(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 初始化Fragment控件
     */
    private void init(){
        mViewPager=(ViewPager)rootView.findViewById(R.id.home_fragment_mode_pager);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.addOnPageChangeListener(this);
        followMenu=(TextView)rootView.findViewById(R.id.home_fragment_navigator_bar_follow_menu);
        followMenu.setOnClickListener(this);
        followMenu.setSelected(true);
        hotMenu=(TextView)rootView.findViewById(R.id.home_fragment_navigator_bar_hot_menu);
        hotMenu.setOnClickListener(this);
        discoverMenu=(TextView)rootView.findViewById(R.id.home_fragment_navigator_bar_discover_menu);
        discoverMenu.setOnClickListener(this);
        mAdapter=new HomePagerAdapter(getChildFragmentManager(),TITLE);
        mViewPager.setAdapter(mAdapter);
        goSearchBtn=(RelativeLayout)rootView.findViewById(R.id.home_fragment_navigator_bar_left_view);
        goSearchBtn.setOnClickListener(this);
        chatBtn=(RelativeLayout)rootView.findViewById(R.id.home_fragment_navigator_bar_right_view);
        chatBtn.setOnClickListener(this);
        countView=(TextView)rootView.findViewById(R.id.all_c2c_conversation_un_read_count_view);
    }

    /**
     * 消息数量维护
     */
    private void initMsgCount(){
        MsgCountManager.getInstance().addObserver(this);
        MsgCountManager.getInstance().refreshMsgCount();
    }

    /**
     *更新消息数量
     * @param observable
     * @param data
     */
    @Override
    public void update(Observable observable, Object data) {
        try {
            msgCount=(long)data;
            if(handler!=null){
                handler.sendEmptyMessage(HANDLER_REFRESH_MSG_COUNT);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 刷新指示标志位置
     */
    private void refreshIndicator(int showPosition){
        followMenu.setSelected(false);
        hotMenu.setSelected(false);
        discoverMenu.setSelected(false);
        int position=mViewPager.getCurrentItem();
        if(position!=showPosition){
            mViewPager.setCurrentItem(showPosition,true);
        }
        switch (showPosition){
            case 0:
                followMenu.setSelected(true);
                break;
            case 1:
                hotMenu.setSelected(true);
                break;
            case 2:
                discoverMenu.setSelected(true);
                break;
            default:
                break;
        }
    }

    /**
     * 查找功能
     */
    private void goSearch(){
        Intent search=new Intent(getActivity(), UserSearch.class);
        startActivity(search);
    }


    /**
     * 显示私信会话列表界面
     */
    public void showC2CListWin(){
        Intent c2cList=new Intent(getContext(),C2CConversationManager.class);
        startActivity(c2cList);
    }

    @Override
    public void handleMsg(Message msg) {
        super.handleMsg(msg);
        switch (msg.what){
            case HANDLER_REFRESH_MSG_COUNT:
                updateMsgCount();
                break;
        }
    }

    /**
     * 更新消息未读数
     */
    public void updateMsgCount() {
        if(countView!=null){
            if(msgCount==0){
                countView.setVisibility(View.INVISIBLE);
            }else{
                countView.setVisibility(View.VISIBLE);
            }
            countView.setText(msgCount+"");
        }
    }

    /**
     * 显示热门界面
     */
    @Subscribe
    public void onShowHot(OnShowHotFragment event){
        if(handler!=null){
            handler.post(new Runnable() {
                @Override
                public void run() {
                    hotMenu.performClick();
                }
            });
        }
    }

    @Override
    public void onDestroyView() {
        MsgCountManager.getInstance().deleteObserver(this);
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }
}
